//
//  XZGradeView.m
//  Teecloud
//
//  Created by PC on 2018/12/5.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZGradeView.h"
@interface XZGradeView ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *alertView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *gradeArr;
@end
@implementation XZGradeView
- (void)awakeFromNib{
    [super awakeFromNib];
    /* 滚动指定段的指定row  到 指定位置*/
    
    self.tableView.delegate= self;
    self.tableView.dataSource = self;
    [self.gradeArr removeAllObjects];
    for (int i = 1; i < 101; i ++) {
        NSString *str = [NSString stringWithFormat:@"%d分",i];
        [self.gradeArr addObject:str];
    }
    //滚动到指定的行
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:55 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (NSMutableArray *)gradeArr{
    if (!_gradeArr) {
        _gradeArr = [[NSMutableArray alloc]init];
    }
    return _gradeArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.gradeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    if (indexPath.row == 59) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.textLabel.textColor = k1168E6Color;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = self.gradeArr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    NSString *grade = self.gradeArr[indexPath.row];
    if (self.gradeViewBlock) {
        self.gradeViewBlock(grade);
        [self hideView];
    }
}
/**
 取消

 @param sender 取消按钮
 */
- (IBAction)cancleButtonAction:(UIButton *)sender {
    [self hideView];
}

- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
        [rootWindow addSubview:self];
//        [self animationAlert:self];
    }];
}

- (void)hide{
    [UIView animateWithDuration:0.25 animations:^{
        [self removeFromSuperview];
    }];
    
}
- (void)hideView{
    [UIView animateWithDuration:0.25 animations:^{
        [self removeFromSuperview];
    }];
}
- (void)animationAlert:(UIView *)view
{
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.4;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:popAnimation forKey:nil];
    
}
@end
